package Lee_code;

public class Lee_24 {
     public class ListNode {
         int val;
         ListNode next;
         ListNode() {}
         ListNode(int val) { this.val = val; }
         ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     }

    public ListNode swapPairs(ListNode head) {
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode dummy2 = dummy;

        if(head == null || head.next == null)
            return head;
        ListNode first = dummy.next;
        ListNode second = dummy.next.next;
        while(second != null && second.next != null){
            ListNode temp = second.next;
            first.next = temp;
            second.next  =first;
            dummy2.next = second;
            dummy2 = first;
        }
        return dummy.next;
    }
}
